package dg;

import java.util.*;

public class Bayes {
    public static void main(String[] args) {
        //age   youth = 1, middle_aged = 2,senior = 3
        //income  low = 1,medium = 2,high = 3
        //student yes = 1，no = 2
        //credit_rating fair = 1,  excellent = 2
        //buys_computer yes = 1 no = 2
        int items[][] = {{1, 3, 2, 1, 2},
                {1, 3, 2, 2, 2},
                {2, 3, 2, 1, 1},
                {3, 2, 2, 1, 1},
                {3, 1, 1, 1, 1},
                {3, 1, 1, 2, 2},
                {2, 1, 1, 2, 1},
                {1, 2, 2, 1, 2},
                {1, 1, 1, 1, 1},
                {3, 2, 1, 1, 1},
                {1, 2, 1, 2, 1},
                {2, 2, 2, 2, 1},
                {2, 3, 1, 1, 1},
                {3, 2, 2, 2, 2}};

        List<String> conditionList = new ArrayList<>();
        conditionList.add(0 + "_" + 1);
        conditionList.add(1 + "_" + 2);
        conditionList.add(2 + "_" + 1);
        conditionList.add(3 + "_" + 1);

        Integer result = 4;

        Set<Integer> resultSet = new HashSet<>();

        int row = items.length;
        int col = items[0].length;

        Map<String, Integer> map = new HashMap<>();
        for (int i = 0; i < col; i++) {
            for (int j = 0; j < row; j++) {
                String key;
                if (i != result) {
                    key = items[j][result] + "_" + i + "_" + items[j][i];
                } else {
                    key = i + "_" + items[j][i];
                    resultSet.add(items[j][i]);
                }
                Integer count = map.get(key);
                if (count == null) {
                    map.put(key, 1);
                } else {
                    map.put(key, ++count);
                }
            }
        }

        for (Integer type : resultSet) {
            Float mul = null;
            //除数
            Integer divisor = map.get(result + "_" + type);
            for (String condition : conditionList) {
                Integer dividend = map.get(type + "_" + condition);
                if (mul == null) {
                    //被除数
                    mul = dividend / (float) divisor;
                } else {
                    mul = mul * (dividend / (float) divisor);
                }
            }
        }
    }
}
